import Vue from 'vue';
import vuex from 'vuex'
import * as types from './mutation-type.js';
Vue.use(vuex);
export default new vuex.Store({ //store对象
  //存放组件之间共享的数据
  state: {
    authList: [],
    messgelist: []
  },
  //显式的更改state里的数据
  mutations: {
    // 设置权限列表
    [types.SET_AUTHLIST](state, authList) {
      state.authList = authList ? authList : []
    },
    //设置消息列表
    [types.SET_MESSGELIST](state, messgelist) {
      state.messgelist = messgelist ? messgelist : []
    }
  },
  actions: {
    setAuthListAsyn({
      commit
    }, authList) {
      commit(types.SET_AUTHLIST, authList)
    },
    setMessgeListAsyn({
      commit
    }, messgelist) {
      commit(types.SET_MESSGELIST, messgelist)
    }
  },
  //获取数据的方法
  getters: {
    getAuthList(state) {
      let _authList = state.authList.length ? state.authList : JSON.parse(sessionStorage.getItem('Menu'))
      let authIdList = [];
      for (let i = 0; i < _authList.length; i++) {
        authIdList.push(_authList[i].functionId)
      }
      return authIdList
    },
    getMessgeList(state) {
      return state.messgelist
    }
  }
})
